<?php
Zend::loadClass('Lead_EDO_Datatype_Base');

/**
 * A concrete field. Is represented in database by a column of a specific datatype.
 *
 */
abstract class Lead_EDO_Datatype_Field extends Lead_EDO_Datatype_Base
{
    
    private $_maxSize = -1;
    private $_notNull;
    
    private $_defaultValue = false;
   
    
    private $_value;
    
    
    
    public function __construct(Lead_EDO_Object_Abstract $owner, $config = array())
    {
        parent::__construct($owner, $config);
        
                
        if(key_exists('size', $config))
            $this->setMaxSize($config['size']);
        
        $this->setNotNull((isset($config['not_null'])) ? (bool) $config['not_null'] : false);
                  
    
    
        if(isset($config['default']))
        {
            $this->setDefaultValue($config['default']);
            
            // why?
            $this->setValue($config['default']);
        }
        
    
    }

   
     /**
     * Returns field's table in database.
     *
     * @return string
     */
    public function getTable()
    {
        return $this->getOwner()->getFieldsTable($this->getName());
    }

    
    
   	public function setMaxSize($maxSize)
	{
	    $this->_maxSize = $maxSize;
	}


	public function getMaxSize()
	{
	    return $this->_maxSize;
	}


	public function setNotNull($nullBool)
	{
	    $this->_notNull = $nullBool;
	}


	public function notNull()
	{
	    return $this->_notNull;
	}

	
	public function setDefaultValue($defaultValue)
	{
	    $this->_defaultValue = $defaultValue;
	}
	
	
	public function getDefaultValue()
	{
	    return $this->_defaultValue;
	}
	
	
	/**
	 * Returns field's value.
	 *
	 * @todo This gotta change!
	 * @return mixed
	 */
	public function getValue()
	{
		return $this->_value;
	}


    public function setValue($value, $validate = true)
    {
                
        if($validate)
            $value = $this->validate($value);
            
    
        if($value !== false)
        {
            $oldValue = $this->_value;
            
            $this->_value = $value;
            
            if($this->_value !== $oldValue)
            {
              
                $this->triggerEvent('onChange');
            }

        }
        
        return ($value === false) ? false : true;
    
    }
    
	

	/**
	 * Returns field's value to display on screen
	 *
	 * @todo This gotta change. This in fact is __toString() ?
	 * @return string
	 */
	public function getDisplayValue()
	{
	    return $this->getValue();
	}
	
	



    
    
}